** Figure 7 **
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"
global Appendixresultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results\appendix"

* 1. Finalize sample & variables
use "$Section4/outcomes_state.dta", clear

merge 1:1 product_module_code quality_rank fips using "$Section4/instrument_age_educ_race_children_loostate"
keep if _merge==3
drop _merge

gen double RMS_ltornqvist_price_index_win  = log(RMS_tornqvist_price_index_win)
gen double RMS_lpaasche_price_index_win=log(RMS_paasche_price_index_win)
gen double RMS_llaspeyres_price_index_win=log(RMS_laspeyres_price_index_win)
gen double RMS_lces_price_index_win=log(RMS_ces_price_index_win)

* for RMS, use all years except 2010
gen RMS_avg_spending = (RMS_total_spending_QM_2006 + RMS_total_spending_QM_2007 + RMS_total_spending_QM_2008 ///
 + RMS_total_spending_QM_2009 + RMS_total_spending_QM_2011 + RMS_total_spending_QM_2012 ///
 + RMS_total_spending_QM_2013 + RMS_total_spending_QM_2014 + RMS_total_spending_QM_2015)/9 

gen RMS_cum_spending_growth = (RMS_total_spending_QM_2014+RMS_total_spending_QM_2015)/(RMS_total_spending_QM_2006+RMS_total_spending_QM_2007) 
gen RMS_avg_spending_growth_pc = (RMS_cum_spending_growth^(1/(2015-2007)) - 1)*100
sum RMS_avg_spending_growth_pc [aw=RMS_avg_spending], d

gen RMS_avg_infl_full = (RMS_fullces_pi_win_common-1)*100
sum RMS_avg_infl_full [aw=RMS_avg_spending], d

gen RMS_avg_realspending_growth_pc = RMS_avg_spending_growth_pc - RMS_avg_infl_full
sum RMS_avg_realspending_growth_pc [aw=RMS_avg_spending], d
gen p5_realgrowth=r(p5)
gen p95_realgrowth=r(p95)

gen RMS_avg_tornqvist_infl = (RMS_tornqvist_price_index_win - 1)*100

* keep track of variables in average log change (for IV)
gen RMS_cum_Dlog_spending = log( (RMS_total_spending_QM_2014+RMS_total_spending_QM_2015)/2 ) ///
- log( (RMS_total_spending_QM_2006+RMS_total_spending_QM_2007)/2 )
gen RMS_avg_Dlog_spending = RMS_cum_Dlog_spending/(2015-2007)
sum RMS_avg_Dlog_spending [aw=RMS_avg_spending], d

gen RMS_avg_Dlog_realspending = RMS_avg_Dlog_spending - RMS_lfullces_pi_win_common
sum RMS_avg_Dlog_realspending [aw=RMS_avg_spending], d
gen p5_Dlog_realspending=r(p5)
gen p95_Dlog_realspending=r(p95)

* document analysis sample
gen analysis_sample=1
replace analysis_sample=0 if ///
missing(inst_raw) | ///
missing(RMS_avg_spending) | ///
missing(RMS_avg_realspending_growth_pc) | ///
missing(RMS_feenstra_win) | ///
missing(RMS_avg_tornqvist_infl) | ///
missing(RMS_avg_infl_full) | ///
missing(inst_linageres) | ///
missing(inst_raw_lvl) | ///
missing(RMS_total_spending_QM_2006)
sum analysis_sample, d
sum analysis_sample [aw=RMS_avg_spending], d
distinct product_module_code if analysis_sample==1
* have 693 product modules
keep if analysis_sample==1
save "$Section4/analysis_main_state.dta", replace

* generate identifiers for outliers (with RMS spending weights)
foreach i in RMS_avg_Dlog_realspending ///
RMS_feenstra_win ///
RMS_ltornqvist_price_index_win ///
RMS_lfullces_pi_win_common  ///
inst_raw inst_raw_lvl inst_linageres inst_ageres inst_ageeducres ///
inst_ageeducstateres inst_allres inst_educres inst_stateres ///
inst_linageducres inst_linagestateres inst_linagerace inst_linagechild ///
inst_raceres inst_childrenres { 
gen `i'_o=1
sum `i' [aw=RMS_avg_spending], d
gen `i'_w=`i'
replace `i'_w=r(p95) if `i'_w>r(p95)
replace `i'_w=r(p5) if `i'_w<r(p5)
replace `i'_o=0 if inrange(`i',r(p5),r(p95))
}

* generate measure of "initial" market size
gen lRMS_total_spending_QM_2006=log(RMS_total_spending_QM_2006)
drop if missing(lRMS_total_spending_QM_2006) | missing(inst_raw_lvl) ///
| missing(RMS_ltornqvist_price_index_win) | missing(RMS_avg_Dlog_realspending) ///
| missing(inst_linageres_w) | missing(RMS_lfullces_pi_win_common)

save "$Section4/analysis_main_state.dta", replace


* 2. Produce figures (with regression coefficients) 

* Main results with RMS data

use "$Section4/analysis_main_state.dta", clear

* rescale variables to make them easier to read
foreach i in RMS_avg_Dlog_realspending RMS_avg_Dlog_spending inst_linageres RMS_ltornqvist_price_index_win ///
RMS_lfullces_pi_win_common {
replace `i'=`i'*100
}

* Panel A
reg lRMS_total_spending_QM_2006 inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter lRMS_total_spending_QM_2006 inst_raw_lvl [aw=sqrt(RMS_avg_spending)], nq(100) ///
xtitle("Instrument for Level of Market Size (log points)") ylabel(,angle(0)) ///
ytitle ("Level of Market Size, 2006 (log points)") ///
graphregion(color(white)) note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure6a.pdf", as(pdf) replace
graph save "$resultspath/Figure6a.gph", replace 
 
* Panel B
reg RMS_lces_price_index_win inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter RMS_ltornqvist_price_index_win inst_raw_lvl [aw=sqrt(RMS_avg_spending)], nq(100) ///
xtitle("Instrument for Level of Market Size (log points)") ylabel(,angle(0)) ///
ytitle ("Average CES Inflation" "for Continued Products, 2006-2015 (%)") ///
graphregion(color(white)) note("Coeff. `b'" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure6b.pdf", as(pdf) replace
graph save "$resultspath/Figure6b.gph", replace 
 
* Panel C
reg RMS_lfullces_pi_win_common inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter RMS_lfullces_pi_win_common inst_raw_lvl [aw=sqrt(RMS_avg_spending)] if inst_linageres_o==0, nq(100) ///
xtitle("Instrument for Level of Market Size (log points)") ylabel(,angle(0)) ///
ytitle ("Average CES Inflation with" "Changes in Product Variety, 2006-2015 (%)") ///
graphregion(color(white)) note("Coeff. `b'" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure6c.pdf", as(pdf) replace
graph save "$resultspath/Figure6c.gph", replace 
